﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace LineChartControlTest
{
    /// <summary>
    /// RunningCurve.xaml 的交互逻辑
    /// </summary>
    public partial class RunningCurve : Window
    {
        private double xValue = 0;
        private double xIncrease = 1;
        private double xMaxInterval = 20;
        private DispatcherTimer addPointTimer = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 0, 0, 50) };

        public RunningCurve()
        {
            InitializeComponent();


            addPointTimer.Tick += AddPointTimer_Tick;

            myLineChart.IsLegend = true;
            myLineChart.LegendPosition = LineCharts.Legend.LegendPositionEnum.NorthEast;
        }

        private void AddPointTimer_Tick(object sender, EventArgs e)
        {
            AddData();
        }

        private void AddData()
        {

            if (myLineChart.SeriesCollection.Count <2)
            {
                LineCharts.HorizontalRunningCurve ds = new LineCharts.HorizontalRunningCurve();
                ds.InterpolationMode = LineCharts.InterpolationCurve.InterpolationModeEnum.Linear;
                ds.LineColor = Brushes.Blue;
                ds.LineThickness = 2;
                ds.SeriesName = "Sine-Linear";
                ds.Symbols.SymbolType = LineCharts.Symbols.SymbolTypeEnum.Diamond;
                ds.Symbols.FillColor = Brushes.Transparent;
                ds.Symbols.BorderColor = ds.LineColor;
                myLineChart.SeriesCollection.AddRawData(ds);

                LineCharts.HorizontalRunningCurve ds1 = new LineCharts.HorizontalRunningCurve();
                ds1.InterpolationMode = LineCharts.InterpolationCurve.InterpolationModeEnum.Lagrangian;
                ds1.LineColor = Brushes.Red;
                ds1.LineThickness = 2;
                ds1.SeriesName = "Sine-Barycentric";
                ds1.LinePattern = LineCharts.LinePatternEnum.Dash;
                ds1.Symbols.SymbolType = LineCharts.Symbols.SymbolTypeEnum.Circle;
                ds1.Symbols.FillColor = Brushes.Transparent;
                ds1.Symbols.BorderColor = ds1.LineColor;
                myLineChart.SeriesCollection.AddRawData(ds1);
            }

            if (xValue > myLineChart.XAxis.Max)
            {
                //myLineChart.XMax += 20;
                if ((myLineChart.XMax - myLineChart.XMin) > (xMaxInterval * .95))
                {
                    myLineChart.XMin += 10;
                    myLineChart.XMax += 10;
                }
                else
                {
                    myLineChart.XMax += 10;
                }
            }
            myLineChart.SeriesCollection[0].RawData.Add(xValue, Math.Sin(xValue));
            myLineChart.SeriesCollection[1].RawData.Add(xValue, Math.Sin(xValue));
            myLineChart.SeriesCollection.DrawCurves();

            xValue += xIncrease;
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            addPointTimer.Start();
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            addPointTimer.Stop();
        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            myLineChart.SeriesCollection[0].RawData.Clear();
            myLineChart.SeriesCollection[1].RawData.Clear();
            xValue = 0.0;
            myLineChart.XMin = 0.0;
            myLineChart.XMax = 10.0;
            myLineChart.DrawChartWorkSpace();
        }
    }
}
